// Copyright (C) 2025 Kumo inc.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <string>

#include "test.hpp"

#include <rothar/pegtl/internal/cstring_reader.hpp>

namespace rothar
{
   template< typename Rule, template< typename... > class Action >
   bool parse_cstring( const char* string, const char* source, const std::size_t maximum )
   {
      buffer_input< internal::cstring_reader, eol::lf_crlf, const char*, 1 > in( source, maximum, string );
      return parse< Rule, Action >( in );
   }

   // clang-format off
   struct n : one< 'n' > {};
   struct a : one< 'a' > {};
   struct f : one< 'f' > {};
   struct s : one< 's' > {};

   template< typename Rule > struct my_action {};
   template<> struct my_action< a > : discard_input {};
   template<> struct my_action< f > : discard_input_on_failure {};
   template<> struct my_action< s > : discard_input_on_success {};
   // clang-format on

   void unit_test()
   {
#if defined( __cpp_exceptions )
      ROTHAR_PEGTL_TEST_THROWS( parse_cstring< rep< 4, sor< n, n > >, my_action >( "nnnn", ROTHAR_TEST_LINE, 2 ) );
#endif
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< a, n > >, my_action >( "nnnn", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< f, n > >, my_action >( "nnnn", ROTHAR_TEST_LINE, 2 ) );
#if defined( __cpp_exceptions )
      ROTHAR_PEGTL_TEST_THROWS( parse_cstring< rep< 4, sor< s, n > >, my_action >( "nnnn", ROTHAR_TEST_LINE, 2 ) );
#endif

      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< n, a > >, my_action >( "aaaa", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< a, a > >, my_action >( "aaaa", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< f, a > >, my_action >( "aaaa", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< s, a > >, my_action >( "aaaa", ROTHAR_TEST_LINE, 2 ) );

#if defined( __cpp_exceptions )
      ROTHAR_PEGTL_TEST_THROWS( parse_cstring< rep< 4, sor< n, f > >, my_action >( "ffff", ROTHAR_TEST_LINE, 2 ) );
#endif
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< a, f > >, my_action >( "ffff", ROTHAR_TEST_LINE, 2 ) );
#if defined( __cpp_exceptions )
      ROTHAR_PEGTL_TEST_THROWS( parse_cstring< rep< 4, sor< f, f > >, my_action >( "ffff", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_THROWS( parse_cstring< rep< 4, sor< s, f > >, my_action >( "ffff", ROTHAR_TEST_LINE, 2 ) );
#endif

      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< n, s > >, my_action >( "ssss", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< a, s > >, my_action >( "ssss", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< f, s > >, my_action >( "ssss", ROTHAR_TEST_LINE, 2 ) );
      ROTHAR_PEGTL_TEST_ASSERT( parse_cstring< rep< 4, sor< s, s > >, my_action >( "ssss", ROTHAR_TEST_LINE, 2 ) );
   }

}  // namespace rothar

#include "main.hpp"
